//
//  FirstViewController.m
//  RBIOSBase
//
//  Created by ren on 09/11/2017.
//  Copyright © 2017 bryan. All rights reserved.
//

#import "FirstViewController.h"
#import "TestMRCViewController.h"
#import "InfiniteScrollView.h"

@interface FirstViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic, strong) UIView *popView;
@property (nonatomic, strong) InfiniteScrollView *infiniteScrollView;

@end

@implementation FirstViewController

- (void)dealloc
{
	if (self.infiniteScrollView) {
		[self.infiniteScrollView->curTimer invalidate];
	}
	NSLog(@"-----FirstViewController");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//	__strong UIView *view = [UIView new];
//	__weak UIView *bView = view;
//	NSLog(@"%ld",CFGetRetainCount((__bridge void *)view));
//	NSLog(@"%p",bView);
	[self addInfiniteScrollView];
}

- (void)addInfiniteScrollView
{
	NSArray *imageList = [NSArray arrayWithObjects:[UIImage imageNamed:@"me"], [UIImage imageNamed:@"test"], [UIImage imageNamed:@"timg"], [UIImage imageNamed:@"test1"], [UIImage imageNamed:@"test2"], nil];//
	if (!self.infiniteScrollView) {
		self.infiniteScrollView = [[InfiniteScrollView alloc] init];
	}
	[self.infiniteScrollView setImageList:imageList];
	[self.infiniteScrollView setScrollStyle:0 autoScroll:NO];
	[self.view addSubview:self.infiniteScrollView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellTest"];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellTest"];
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		[button setFrame:CGRectMake(20, 2, 40, 30)];
		button.tag = indexPath.row;
		[button setTitle:@"按钮" forState:UIControlStateNormal];
		[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
		[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
		[cell.contentView addSubview:button];
	}
//	if (indexPath.row % 10 == 0) {
//		usleep(300 * 1000);
//		[cell.textLabel setText:[NSString stringWithFormat:@"耗时的cell--->%ld", indexPath.row]];
//	}
//	else {
//		[cell.textLabel setText:[NSString stringWithFormat:@"%ld", indexPath.row]];
//	}
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//	if (_popView == nil) {
//		_popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
//		[_popView setBackgroundColor:[UIColor redColor]];
//
//		UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//		CGPoint point = [tableView convertPoint:_popView.frame.origin fromView:cell.contentView];
//
//		NSLog(@"point.x--->%lf,point.y----->%lf,self.tableview.contentoffset.x--->%lf,self.tableview.contentoffset.y---->%lf", point.x, point.y, self.tableview.contentOffset.x, self.tableview.contentOffset.y);
//
//		[_popView setFrame:CGRectMake(point.x, point.y, _popView.frame.size.width, _popView.frame.size.height)];
//		[tableView addSubview:_popView];
//	}
	
	TestMRCViewController *testMRC = [TestMRCViewController new];
	[self.navigationController pushViewController:testMRC animated:YES];
	if (_popView) {
		[_popView removeFromSuperview];
		_popView = nil;
	}
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
	if (_popView) {
		[_popView removeFromSuperview];
		_popView = nil;
	}
}

- (void)buttonAction:(UIButton *)sender
{
	if (_popView == nil) {
		_popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
		[_popView setBackgroundColor:[UIColor redColor]];
		NSUInteger tag = sender.tag;
	
		NSIndexPath *indexPath = [NSIndexPath indexPathForItem:tag inSection:0];
	
		UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
		CGPoint point = [self.tableview convertPoint:_popView.frame.origin fromView:cell.contentView];
	
	
		NSLog(@"point.x--->%lf,point.y----->%lf,self.tableview.contentoffset.x--->%lf,self.tableview.contentoffset.y---->%lf", point.x, point.y, self.tableview.contentOffset.x, self.tableview.contentOffset.y);
		
		[_popView setFrame:CGRectMake(point.x, point.y, _popView.frame.size.width, _popView.frame.size.height)];
		[self.tableview addSubview:_popView];
	}
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 44.0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
